﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SIGCBI.Entities;
using System.Data;
using System.Data.Entity;

namespace SIGCBI.Repository
{
    public interface IAlquilerRepository
    {
        List<ALQUILER> GetAllAlquilerByFechas(DateTime fecIngreso, DateTime fecSalida);
        ALQUILER GetFromAlquierById(Int32 id);
        void AddAlquiler(ALQUILER alquiler);
        void UpdateAlquiler(ALQUILER alquiler);
    }

    public class AlquilerRepository : MasterDataAccess, IAlquilerRepository
    {
        public List<ALQUILER> GetAllAlquilerByFechas(DateTime fecIngreso, DateTime fecSalida)
        {
           //var query = (from a in Context.ALQUILER
           //              where fecIngreso >=a.fecSalida
           //              orderby a.fecSalida descending
           //              group a by a.IdHabitacion into gaa
           //                  select gaa).First();
                             
           // List<ALQUILER> alquiler = query.ToList();

           // return alquiler;

            var query = from a in Context.ALQUILER
                        orderby a.fecSalida ascending
                        group a by a.IdHabitacion into aa
                        //where fecIngreso >= a.fecSalida 
                        select aa.FirstOrDefault();

            return query.ToList();

        }

        public ALQUILER GetFromAlquierById(int id)
        {
            var query = from p in Context.ALQUILER
                        where p.ID.Equals(id)
                        select p;

            return query.SingleOrDefault();
        }

        public void AddAlquiler(ALQUILER alquiler)
        {
            Context.ALQUILER.Add(alquiler);
            Context.SaveChanges();
        }

        public void UpdateAlquiler(ALQUILER alquiler)
        {
            Context.ALQUILER.Attach(alquiler);
            Context.Entry(alquiler).State = EntityState.Modified;
            Context.SaveChanges();
        }
    }
}
